/*
 * BoissonManager.java
 *
 * Created on 1 novembre 2007, 17:45
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.ifips5.nal.epizza.web;

import com.ifips5.nal.epizza.util.JDBCManager;
import com.ifips5.nal.epizza.web.model.Boisson;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.sql.rowset.WebRowSet;

/**
 *
 * @author Moi
 */
public class BoissonManager
{
    
    private BeanManager beanManager = null;
    private List<Boisson> boissonList = new LinkedList();
    private Map<Integer, Boisson> boissonMap = new HashMap();
    private Boisson currentBoisson = null;
    
    
    
    
    /** Creates a new instance of BoissonManager */
    public BoissonManager()
    {
    }
    
    public BoissonManager(BeanManager beanManager)
    {
        this.beanManager = beanManager;
    }
    
    
    public void loadBoisson(String id)
    {
        String command = "SELECT * FROM boisson WHERE idBoisson = "+id;
        int[] keyCol = {1};
        WebRowSet wrs =  JDBCManager.getInstance().getRowSet(command, keyCol);
        try
        {
            if(wrs.next())
            {
                this.currentBoisson = new Boisson(wrs.getInt(1), wrs.getString(2), wrs.getDouble(3));
            }
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }
    
    
    public void loadBoissons()
    {
      String command = "SELECT * FROM boisson";
        int[] keyCol = {1};

        WebRowSet wrs =  JDBCManager.getInstance().getRowSet(command, keyCol);
        try
        {
            while(wrs.next())
            {
                Boisson currentBoisson = new Boisson(wrs.getInt(1), wrs.getString(2), wrs.getDouble(3));
                boissonMap.put(wrs.getInt(1), currentBoisson);
                boissonList.add(currentBoisson);
            }
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }           
    }

    
    public void deleteBoisson(String idBoisson)
    {
        try
        {
            Connection connection = JDBCManager.getInstance().getConnection();
            Statement stmt = connection.createStatement();
            stmt.addBatch("DELETE FROM menu_has_boisson WHERE Boisson_idBoisson="+idBoisson);
            stmt.addBatch("DELETE FROM boisson WHERE idBoisson=\""+idBoisson+"\"");
            stmt.executeBatch();   
            connection.commit();
            connection.close();    
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        beanManager.updatePDF();
    }
    
    
    public void deleteBoisson(String[] delBoissons)
    {
        try
        {
            Connection connection = JDBCManager.getInstance().getConnection();
            Statement stmt = connection.createStatement();
            
            for(int i = 0; i < delBoissons.length ; i++)
            {
                stmt.addBatch("DELETE FROM menu_has_boisson WHERE Boisson_idBoisson="+delBoissons[i]);
                stmt.addBatch("DELETE FROM Boisson WHERE idBoisson="+delBoissons[i]);

            }

            stmt.executeBatch();   
            connection.commit();
            connection.close();    
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }    
        beanManager.updatePDF();
    }
    
    public void insertBoisson(String name, String prix)
    {
        double dPrix;
        try
        {
            dPrix = Double.parseDouble(prix);
        } catch (NumberFormatException ex)
        {
            dPrix = 1.0;
        }

        try
        {
            Connection connection = JDBCManager.getInstance().getConnection();
            Statement stmt = connection.createStatement();
            stmt.addBatch("INSERT INTO boisson(name, prix) VALUES ( \""+name+"\", "+dPrix+")");
            stmt.executeBatch();   
            connection.commit();
            connection.close();    
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        beanManager.updatePDF();
    }
    
    public void updateBoisson(String id, String name, String prix)
    {
        double dPrix;
        try
        {
            dPrix = Double.parseDouble(prix);
        } catch (NumberFormatException ex)
        {
            dPrix = 1.0;
        }

        try
        {
            Connection connection = JDBCManager.getInstance().getConnection();
            Statement stmt = connection.createStatement();
            stmt.addBatch("UPDATE boisson SET name = \""+name+"\", prix="+dPrix+" WHERE idBoisson="+id);
            stmt.executeBatch();   
            connection.commit();
            connection.close();    
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
        beanManager.updatePDF();
    }
    
    
    public void searchBoisson(String pattern)
    {
        
        String[] tab = pattern.split("\\s");
        
        
        String command = "SELECT * FROM boisson WHERE name LIKE '%"+tab[0]+"%' ";
        
        for (int i = 1; i < tab.length; i++) 
        {
            String currentPattern = tab[i];
            command += "OR name LIKE '%"+currentPattern+"%' ";
        }
        
        int[] keyCol = {1};

        WebRowSet wrs =  JDBCManager.getInstance().getRowSet(command, keyCol);
        try
        {
            while(wrs.next())
            {
                Boisson currentBoisson = new Boisson(wrs.getInt(1), wrs.getString(2), wrs.getDouble(3));
                boissonMap.put(wrs.getInt(1), currentBoisson);
                boissonList.add(currentBoisson);
            }
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }          
    }
    
    
    //////////////////////
    // Getters and Setters
    
    public List<Boisson> getBoissonList()
    {
        return boissonList;
    }

    public void setBoissonList(List<Boisson> boissonList)
    {
        this.boissonList = boissonList;
    }

    public Map<Integer, Boisson> getBoissonMap()
    {
        return boissonMap;
    }

    public void setBoissonMap(Map<Integer, Boisson> boissonMap)
    {
        this.boissonMap = boissonMap;
    }

    public Boisson getCurrentBoisson()
    {
        return currentBoisson;
    }

    public void setCurrentBoisson(Boisson currentBoisson)
    {
        this.currentBoisson = currentBoisson;
    }
    
}
